package com.scalar.chiptrack.adaptor.ispl;

import com.scalar.chiptrack.jobs.JobHandler;
import com.scalar.chiptrack.adaptor.AdaptorConstants;
import com.scalar.chiptrack.adaptor.AdaptorUtils;
import com.scalar.chiptrack.adaptor.ShippedLot;
import com.scalar.chiptrack.adaptor.InventoryLot;
import com.scalar.chiptrack.adaptor.TestLot;
import com.scalar.chiptrack.adaptor.dao.TestAdaptorDAO;
import com.scalar.chiptrack.adaptor.dao.ShippedLotDAO;
import com.scalar.chiptrack.adaptor.dao.InventoryLotDAO;
import com.scalar.chiptrack.adaptor.dao.AdaptorDAO;
import com.scalar.chiptrack.adaptor.dao.AdaptorRunInfo;
import com.scalar.chiptrack.utils.Logger;
import com.scalar.chiptrack.utils.StringUtils;
import com.scalar.chiptrack.db.DBTransaction;
import com.scalar.ScalarException;

import java.util.Date;
import java.util.HashMap;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.TimeZone;
import java.util.Locale;
import java.util.GregorianCalendar;
import java.util.Iterator;

import java.io.File;

import jxl.Workbook;
import jxl.Sheet;
import jxl.Cell;
import jxl.CellType;
import jxl.DateCell;
import jxl.NumberCell;

/**
 * User: ChandraSekhar
 * Date: Sep 5, 2007
 * Time: 1:04:36 PM
 */
public class ISPLDataParser extends JobHandler implements AdaptorConstants
{
    HashMap m_ftDataMap               = new HashMap();
    HashMap m_tnrDataMap                = new HashMap();
    HashMap m_fgDataMap                 = new HashMap();
    HashMap m_shipDataMap               = new HashMap();

    private Logger m_parserLogger       = null;
    private String m_dataOutputDir      = null;
    DBTransaction dbTransaction         = null;

    public ISPLDataParser() throws Exception
    {
        try
        {
            m_dataOutputDir = AdaptorUtils.getDataOutputDir( VENDOR_NAME_ISPL, WIP_TYPE_TEST );
            AdaptorUtils.makeAllAdaptorDirs( m_dataOutputDir );
            m_parserLogger = AdaptorUtils.getDataParserLogger( m_dataOutputDir,VENDOR_NAME_ISPL );
            TestAdaptorDAO.setLogger( m_parserLogger);
            InventoryLotDAO.setLogger( m_parserLogger );
            ShippedLotDAO.setLogger(m_parserLogger);
            AdaptorUtils.setLogger( m_parserLogger );
        }
        catch (ScalarException e)
        {
            log( "ERROR!! ERROR!! ERROR!! Exception while loading ISPL output directory details from database, Exception: " + e );
            throw e;
        }
    }

    public static void main(String args[]) throws Exception
    {
        new ISPLDataParser().execute();
    }

    public void execute() throws Exception
    {
        AdaptorUtils.printHeader(VENDOR_NAME_ISPL, CONSTANT_PARSER );

        File doneDestDir = new File(m_dataOutputDir + "/done");

        File wipDataoutDir = new File(m_dataOutputDir);

        File[] fileList = wipDataoutDir.listFiles();

        if ((fileList == null) || (fileList.length <= 3))
        {
            log("No file found in directory: " + wipDataoutDir);
            throw new ScalarException("No file found in directory: " + wipDataoutDir + "----\n\n");
        }

        ArrayList successFileList   = new ArrayList();
        boolean success             = false;

        //PARSING STARTS FROM HERE
        for (int i = 0; i < fileList.length; i++)
        {
            if (fileList[i].isDirectory() == true)
            {
                continue;
            }

            if ( (fileList[i].getName() != null) && fileList[i].getName().trim().startsWith(ISPLPOP3Adaptor.FILE_NAME_WIP ) )
            {
                try
                {
                    ArrayList m_wipStatusList = parseWipInfosheet( fileList[i].getAbsolutePath() );

                    if ( m_wipStatusList != null && m_wipStatusList.size() > 0)
                    {
                        getWipDetails( m_wipStatusList );
                    }
                    successFileList.add( fileList[i] );
                }
                catch( Exception e )
                {
                    success = false;
                    log("Exception while parsing file: " + fileList[i].getAbsolutePath() + "\nException: " + e);
                    e.printStackTrace();
                    throw e;
                }
            }
        }

        /*Updating the Test Lot Information*/
        try
        {
            // Step 1
            // Save Test Information
            if( m_ftDataMap.isEmpty() == false )
            {
                log(" Updating Test Lot Information");
                TestAdaptorDAO.saveTestLot( null, m_ftDataMap.values().iterator(), LOCATION_TYPE_FT, VENDOR_NAME_ISPL );
            }
            else
            {
                TestAdaptorDAO.closeTestLot( null, LOCATION_TYPE_FT, VENDOR_NAME_ISPL );
            }
            m_ftDataMap.clear();
            m_ftDataMap = null;
            success = true;
        }
        catch (ScalarException se)
        {
            success = false;
            se.printStackTrace();
            log("ERROR!! ERROR!! ERROR!! Could not update Test Lot information into database: " + se);
            throw se;
        }

        /*Saving Test Shipment Lots*/
        try
        {
            // Save shipping info -
            if ( m_shipDataMap.isEmpty() == false )
            {
                log( "Updating shipment Information" );
                ShippedLotDAO.saveShippedLot( null, m_shipDataMap.values().iterator() );
            }
        }
        catch ( Exception e )
        {
            success = false;
            log( "ERROR!! ERROR!! ERROR!! Could not update ISPL Test Shipping information into database: " + e );
            e.printStackTrace();
            throw e;
        }

       //Updating the ISPL Tape and Reel Lot Information*/
        try
        {
            // Step 2
            // Save Class Store Information
            if (m_tnrDataMap.isEmpty() == false)
            {
                log("Updating ISPL Tape and Reel Lot Information");
                InventoryLotDAO.saveInventoryLot(null, m_tnrDataMap.values().iterator(), LOCATION_TYPE_TAPE_REEL, VENDOR_NAME_ISPL);
            }
            else
            {
                InventoryLotDAO.closeInventoryLot(dbTransaction, LOCATION_TYPE_TAPE_REEL, VENDOR_NAME_ISPL);
            }
            m_tnrDataMap.clear();
            m_tnrDataMap = null;
            success = true;
        }
        catch (ScalarException se)
        {
            success = false;
            se.printStackTrace();
            log("ERROR!! ERROR!! ERROR!! Could not update Tape and Reel Lot information into database: " + se);
            throw se;
        }

        //Updating the ISPL FG Lot Information*/
        try
        {
            // Step 2
            // Save FG Lot Information
            if (m_fgDataMap.isEmpty() == false)
            {
                log(" Updating ISPL FG Lot Information");
                InventoryLotDAO.saveInventoryLot(null, m_fgDataMap.values().iterator(), LOCATION_TYPE_FG, VENDOR_NAME_ISPL);
            }
            else
            {
                InventoryLotDAO.closeInventoryLot(null, LOCATION_TYPE_FG, VENDOR_NAME_ISPL);
            }
            m_fgDataMap.clear();
            m_fgDataMap = null;
            success = true;
        }
        catch (ScalarException se)
        {
            success = false;
            se.printStackTrace();
            log("ERROR!! ERROR!! ERROR!! Could not update FG Lot information into database: " + se);
            throw se;
        }

        // TO SEND THE NEW LOTS, NEW TRANSIT LOTS, NO PO LOTS, DISCREPANCY LOTS NOTIFICATIONS TO THE USER
        AdaptorDAO.sendAllNotifications( VENDOR_NAME_ISPL, true, true, true );

         // To Close the Drop Out Lots
        AdaptorDAO.closeDropOutLots( VENDOR_NAME_ISPL );

        int adaptor_id = AdaptorRunInfo.getLastRunAdaptorId( ISPLPOP3Adaptor.ADAPTOR_NAME, CONSTANT_ADAPTOR);
        // Update Adaptor_Run_Info table
        try
        {
            if ( success == true )
            {
                AdaptorRunInfo.saveAdaptorLastUpdatedStatus(CONSTANT_PARSER, ISPLPOP3Adaptor.ADAPTOR_NAME, CONSTANT_SUCCESS, adaptor_id);
                System.out.println( "SUCCESSFULLY UPDATED IN THE DATABASE" );
            }
        }
        catch ( ScalarException se )
        {
            success = false; //As filES DATA SAVED , UPDATEING AS SUCCESS
            AdaptorRunInfo.saveAdaptorLastUpdatedStatus(CONSTANT_PARSER, ISPLPOP3Adaptor.ADAPTOR_NAME, CONSTANT_SUCCESS, adaptor_id);
            log( "WARNING!! WARNING!! WARNING!! Could not update Adaptor run information, Exception: " + se );
            throw se;
        }
        // Move the successful files to 'done' directory
       if ( success )
       {
           AdaptorUtils.moveFilesToDone( successFileList, doneDestDir );
       }
    }//end of execute method

    /**
     *
     * @param fileName
     * @return
     * @throws Exception
     */
    private ArrayList parseWipInfosheet(String fileName) throws Exception
    {
        if ( fileName == null )
        {
            throw new ScalarException( "Filename argument is null" );
        }

        ISPLWipStatus wipStatus   = null;
        ArrayList wipList           = new ArrayList();

        try
        {
            File wipFedFile = new File( fileName );
            if ( wipFedFile.exists() == false )
            {
                throw new ScalarException( fileName + " doesnt exist" );
            }

            log( "\nFound " + fileName );
            log( "Parsing the file for ISPL Test Wip Report..." );
            System.out.println( "\nFound " + fileName );
            System.out.println( "Parsing the file for ISPL Test Wip Report..." );

            Workbook workbook = Workbook.getWorkbook( wipFedFile );
            Sheet sheet = workbook.getSheet( 0 );

            Cell[] row = null;

            for ( int rowCount = 7; rowCount < sheet.getRows(); rowCount++ )
            {
                row = sheet.getRow( rowCount );

                wipStatus = new ISPLWipStatus();

                if( row.length > 0 )
                {
                    if ( row[0].getType().equals(CellType.EMPTY) )
                    {
                        wipStatus.setItemNo( null );
                    }
                    else
                    {
                        wipStatus.setItemNo( row[0].getContents().trim() );
                    }
                }
                if( row.length > 1)
                {
                    if ( row[1].getType().equals(CellType.EMPTY) )
                    {
                        wipStatus.setReceivedFrom( null );
                    }
                    else
                    {
                        wipStatus.setReceivedFrom( row[1].getContents().trim() ) ;
                    }
                }
                if( row.length > 2)
                {
                    if ( row[2].getType().equals(CellType.EMPTY) )
                    {
                        wipStatus.setLocation( null );
                    }
                    else
                    {
                        wipStatus.setLocation( row[2].getContents().trim() );
                    }
                }
                if( row.length > 3)
                {
                    if ( row[3].getType().equals(CellType.EMPTY) )
                    {
                        wipStatus.setPoNo( null );
                    }
                    else
                    {
                        wipStatus.setPoNo( row[3].getContents().trim() );
                    }
                }
                if ( row.length > 4 )
                {
                   if ( row[4].getType().equals(CellType.EMPTY) )
                    {
                        wipStatus.setAirWayBillNo( null );
                    }
                    else
                    {
                        wipStatus.setAirWayBillNo( row[4].getContents().trim() );
                    }
                }
                if( row.length > 5 )
                {
                    if ( row[5].getType().equals(CellType.EMPTY) )
                    {
                        wipStatus.setPartNumber( null );
                    }
                    else
                    {
                        wipStatus.setPartNumber( row[5].getContents().trim() );
                    }
                }
                if( row.length > 6 )
                {
                    if ( row[6].getType().equals(CellType.EMPTY) )
                    {
                        wipStatus.setPkgCode( null );
                    }
                    else
                    {
                        wipStatus.setPkgCode( row[6].getContents().trim() );
                    }
                }
                if ( row.length > 7 )
                {
                    if ( row[7].getType().equals(CellType.EMPTY) )
                    {
                        wipStatus.setLotNumber( null );
                    }
                    else
                    {
                        wipStatus.setLotNumber( row[7].getContents().trim() );
                    }
                }
                if( row.length > 8 )
                {
                    if ( row[8].getType().equals(CellType.EMPTY) )
                    {
                        wipStatus.setInvoiceQty( null );
                    }
                    else
                    {
                        wipStatus.setInvoiceQty( row[8].getContents().trim().replaceAll(",","") );
                    }
                }
                if( row.length > 9 )
                {
                    if ( row[9].getType().equals(CellType.EMPTY) )
                    {
                        wipStatus.setReceiveDate( null );
                    }
                    else if( row[9].getType().equals( CellType.DATE ) )
                    {
                        DateCell dc = (DateCell) row[9];
                        java.util.Date dateIn = null;
                        dateIn = dc.getDate();

                        if ("America/Los_Angeles".equals(System.getProperty("user.timezone")))
                        {
                            Calendar cal = Calendar.getInstance( TimeZone.getTimeZone( "GMT" ), Locale.US );
                            cal.setTime(dateIn);
                            dateIn = new GregorianCalendar(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DATE)).getTime();
                            wipStatus.setReceiveDate( StringUtils.formatDateToString( dateIn )  ) ;
                        }
                        else
                        {
                            Calendar cal = new GregorianCalendar();
                            cal.setTime(dateIn);
                            dateIn = new GregorianCalendar(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DATE)).getTime();
                            wipStatus.setReceiveDate(StringUtils.formatDateToString(dateIn));
                        }
                    }
                    else
                    {
                        Calendar cal = new GregorianCalendar();
                        java.util.Date dateIn = null;
                        cal.setTime( new Date() );
                        dateIn = new GregorianCalendar(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DATE)).getTime();
                        wipStatus.setReceiveDate(StringUtils.formatDateToString(dateIn));
                    }
                }
                if( row.length >10 )
                {
                    if ( row[10].getType().equals(CellType.EMPTY) )
                    {
                        wipStatus.setExpectedDate( null );
                    }
                    else if( row[10].getType().equals( CellType.DATE ) )
                    {
                        DateCell dc = (DateCell) row[10];
                        java.util.Date dateIn = null;
                        dateIn = dc.getDate();

                        if ("America/Los_Angeles".equals(System.getProperty("user.timezone")))
                        {
                            Calendar cal = Calendar.getInstance( TimeZone.getTimeZone( "GMT" ), Locale.US );
                            cal.setTime(dateIn);
                            dateIn = new GregorianCalendar(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DATE)).getTime();
                            wipStatus.setExpectedDate( StringUtils.formatDateToString( dateIn )  ) ;
                        }
                        else
                        {
                            Calendar cal = new GregorianCalendar();
                            cal.setTime(dateIn);
                            dateIn = new GregorianCalendar(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DATE)).getTime();
                            wipStatus.setExpectedDate(StringUtils.formatDateToString(dateIn));
                        }
                    }
                    else
                    {

                        wipStatus.setExpectedDate( null ) ;
                    }
                }
                if( row.length > 11 )
                {
                    if ( row[11].getType().equals(CellType.EMPTY) )
                    {
                        wipStatus.setExpectedShipDate( null );
                    }
                    else if( row[11].getType().equals( CellType.DATE ) )
                    {
                        DateCell dc = (DateCell) row[11];
                        java.util.Date dateIn = null;
                        dateIn = dc.getDate();

                        if ("America/Los_Angeles".equals(System.getProperty("user.timezone")))
                        {
                            Calendar cal = Calendar.getInstance( TimeZone.getTimeZone( "GMT" ), Locale.US );
                            cal.setTime(dateIn);
                            dateIn = new GregorianCalendar(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DATE)).getTime();
                            wipStatus.setExpectedShipDate( StringUtils.formatDateToString( dateIn )  ) ;
                        }
                        else
                        {
                            Calendar cal = new GregorianCalendar();
                            cal.setTime(dateIn);
                            dateIn = new GregorianCalendar(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DATE)).getTime();
                            wipStatus.setExpectedShipDate(StringUtils.formatDateToString(dateIn));
                        }
                    }

                }
                if( row.length >12 )
                {
                    if ( row[12].getType().equals(CellType.EMPTY) )
                    {
                        wipStatus.setActualShipDate( null );
                    }
                    else if( row[12].getType().equals( CellType.DATE ) )
                    {
                        DateCell dc = (DateCell) row[12];
                        java.util.Date dateIn = null;
                        dateIn = dc.getDate();

                        if ("America/Los_Angeles".equals(System.getProperty("user.timezone")))
                        {
                            Calendar cal = Calendar.getInstance( TimeZone.getTimeZone( "GMT" ), Locale.US );
                            cal.setTime(dateIn);
                            dateIn = new GregorianCalendar(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DATE)).getTime();
                            wipStatus.setActualShipDate( StringUtils.formatDateToString( dateIn )  ) ;
                        }
                        else
                        {
                            Calendar cal = new GregorianCalendar();
                            cal.setTime(dateIn);
                            dateIn = new GregorianCalendar(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DATE)).getTime();
                            wipStatus.setActualShipDate(StringUtils.formatDateToString(dateIn));
                        }
                    }
                    else
                    {

                        wipStatus.setActualShipDate( null ) ;
                    }
                }
                if ( row.length > 13 )
                {
                    if ( row[13].getType().equals(CellType.EMPTY) )
                    {
                        wipStatus.setCommitDate( null );
                    }
                    else if( row[13].getType().equals( CellType.DATE) )
                    {
                        DateCell dc = (DateCell) row[13];
                        java.util.Date dateIn = null;
                        dateIn = dc.getDate();

                        if ("America/Los_Angeles".equals(System.getProperty("user.timezone")))
                        {
                            Calendar cal = Calendar.getInstance( TimeZone.getTimeZone( "GMT" ), Locale.US );
                            cal.setTime(dateIn);
                            dateIn = new GregorianCalendar(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DATE)).getTime();
                            wipStatus.setCommitDate( StringUtils.formatDateToString( dateIn )  ) ;
                        }
                        else
                        {
                            Calendar cal = new GregorianCalendar();
                            cal.setTime(dateIn);
                            dateIn = new GregorianCalendar(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DATE)).getTime();
                            wipStatus.setCommitDate(StringUtils.formatDateToString(dateIn));
                        }
                    }
                    else
                    {
                        wipStatus.setCommitDate( null );
                    }
                }
                if( row.length > 14 )
                {
                    if ( row[14].getType().equals(CellType.EMPTY) )
                    {
                        wipStatus.setForTest( null );
                    }
                    else if( row[14].getType().equals( CellType.NUMBER) )
                    {
                        NumberCell nc = (NumberCell) row[14];
                        wipStatus.setForTest(nc.getValue()+"");
                    }
                    else
                    {
                        wipStatus.setForTest( row[14].getContents().trim().replaceAll(",","") );
                    }
                }
                if( row.length > 15 )
                {
                    if ( row[15].getType().equals(CellType.EMPTY) )
                    {
                        wipStatus.setTestInQty( null );
                    }
                    else
                    {
                        wipStatus.setTestInQty( row[15].getContents().trim().replaceAll(",","") );
                    }
                }
                if( row.length >16 )
                {
                    if ( row[16].getType().equals(CellType.EMPTY) )
                    {
                        wipStatus.setTestOutQty( null );
                    }
                    else
                    {
                        wipStatus.setTestOutQty( row[16].getContents().trim().replaceAll(",","") );
                    }
                }
                if( row.length >17 )
                {
                    if ( row[17].getType().equals(CellType.EMPTY) )
                    {
                        wipStatus.setYield( null );
                    }
                    else
                    {
                        wipStatus.setYield( row[17].getContents().trim() );
                    }
                }
                if( row.length >19 )
                {
                    if ( row[19].getType().equals(CellType.EMPTY) )
                    {
                        wipStatus.setForTnr( null );
                    }
                    else
                    {
                        wipStatus.setForTnr( row[19].getContents().trim().replaceAll(",","") );
                    }
                }
                if( row.length >20 )
                {
                    if ( row[20].getType().equals(CellType.EMPTY) )
                    {
                        wipStatus.setTnrIn( null );
                    }
                    else
                    {
                        wipStatus.setTnrIn( row[20].getContents().trim().replaceAll(",","") );
                    }
                }
                if( row.length >21 )
                {
                    if ( row[21].getType().equals(CellType.EMPTY) )
                    {
                        wipStatus.setTnrOut( null );
                    }
                    else
                    {
                        wipStatus.setTnrOut( row[21].getContents().trim().replaceAll(",","") );
                    }
                }
                if( row.length >22 )
                {
                    if ( row[22].getType().equals(CellType.EMPTY) )
                    {
                        wipStatus.setTnrYield( null );
                    }
                    else
                    {
                        wipStatus.setTnrYield( row[22].getContents().trim() );
                    }
                }
                if( row.length >23 )
                {
                    if ( row[23].getType().equals(CellType.EMPTY) )
                    {
                        wipStatus.setForPack( null );
                    }
                    else
                    {
                        wipStatus.setForPack( row[23].getContents().trim().replaceAll(",","") );
                    }
                }
                // 24 Newly Added Column FG not Required
                if( row.length >25 )
                {
                    if ( row[25].getType().equals(CellType.EMPTY) )
                    {
                        wipStatus.setShipped( null );
                    }
                    else
                    {
                        wipStatus.setShipped( row[25].getContents().trim().replaceAll(",","") );
                    }
                }
                if( row.length >26 )
                {
                    if ( row[26].getType().equals(CellType.EMPTY) )
                    {
                        wipStatus.setRemarks( null );
                    }
                    else
                    {
                        wipStatus.setRemarks( row[26].getContents().trim() );
                    }
                }
                if( row.length >27 )
                {
                    if ( row[27].getType().equals(CellType.EMPTY) )
                    {
                        wipStatus.setReleaseNo( null );
                    }
                    else
                    {
                        wipStatus.setReleaseNo( row[27].getContents().trim() );
                    }
                }
                if ( wipStatus.getPartNumber() != null  && wipStatus.getPartNumber().length() > 0 &&
                        wipStatus.getLotNumber() != null  && wipStatus.getLotNumber().length() > 0 )
                {
                    wipList.add( wipStatus );
                }
            }
        }
        catch ( Exception ex )
        {
            ex.printStackTrace();
            throw ex;
        }

        return wipList;
    }
    /**
     *
     * @param testWipList
     * @throws Exception
     */
    public void getWipDetails( ArrayList testWipList ) throws Exception
    {

        String partNumber       = null;
        String lotNumber        = null;
        String releaseNumber    = null;
        ISPLWipStatus wipStatus = null;
        TestLot testLot         = null;
        InventoryLot tnrLot     = null;
        InventoryLot fgLot      = null;
        ShippedLot shippedLot   = null;

        try
        {
           Iterator testWipIterator = testWipList.iterator();

            while ( testWipIterator.hasNext() )
            {
                wipStatus = (ISPLWipStatus) testWipIterator.next();

                partNumber = wipStatus.getPartNumber();
                lotNumber = wipStatus.getLotNumber();
                releaseNumber = wipStatus.getReleaseNo();
                if ( releaseNumber == null )
                {
                    releaseNumber =  lotNumber;
                }
                if ( releaseNumber != null && releaseNumber.length() < 7 && releaseNumber.startsWith("0") == false )
                {
                    releaseNumber = "0"+ releaseNumber;
                }

                if ( releaseNumber != null && releaseNumber.length() > 0 )
                {
                    if ( releaseNumber.indexOf("-") != -1 )
                    {
                        releaseNumber = releaseNumber.substring(0, releaseNumber.indexOf("-") );
                    }

                    int releaseCount = AdaptorDAO.getApprovedAssemblyReleaseLot( releaseNumber, partNumber, VENDOR_NAME_ISPL );

                    if ( releaseCount > 0 )
                    {
                        AdaptorDAO.updateAssemblyReleaseStatus( releaseNumber );
                    }
                }
                /*For test*/
                if ( wipStatus.getLocation() != null && wipStatus.getLocation().equalsIgnoreCase("For Test") == true )
                {
                    log("In Test" + lotNumber );

                    testLot = (TestLot) m_ftDataMap.get(lotNumber + partNumber  );

                    if (testLot == null)
                    {
                        testLot = new TestLot();
                        m_ftDataMap.put(lotNumber + partNumber , testLot);
                    }

                    int quantity = 0;
                    if (testLot.getQtyIn() != null)
                    {
                        try
                        {
                            quantity = Integer.parseInt( testLot.getQtyIn() );
                        }
                        catch (NumberFormatException npe) {
                        }
                        try
                        {
                            quantity += Integer.parseInt( wipStatus.getForTest() );
                        }
                        catch (NumberFormatException npe) {
                        }
                    }
                    else
                    {
                        try
                        {
                            quantity = Integer.parseInt(wipStatus.getForTest() );
                        }
                        catch (NumberFormatException npe) {
                        }
                    }
                    if ( quantity > 0 )
                    {
                        testLot.setLotNumber(lotNumber);
                        testLot.setPartNumber(partNumber);
                        testLot.setDateIn(wipStatus.getReceiveDate());
                        testLot.setQtyIn( quantity+"");
                        testLot.setVendorName(VENDOR_NAME_ISPL );
                        testLot.setStatus( LOT_STATUS_RUNNING );
                        testLot.setPsType( LOCATION_TYPE_FT );
                        testLot.setFabLotNumber( lotNumber );
                        testLot.setPackageType( wipStatus.getPkgCode() );
                        testLot.setExpecetedDate( wipStatus.getExpectedDate() );
                        testLot.setVendorLot( releaseNumber );
                    }
                }
                /*Considering when location not equal to SHIPPED and Remarks equal to Shipped to OSE as Test Shipment Lots*/
                if ( wipStatus.getLocation() != null && wipStatus.getLocation().equalsIgnoreCase("SHIPPED") == false
                      && wipStatus.getRemarks() != null && wipStatus.getRemarks().equalsIgnoreCase("Shipped to OSE"))
                {
                    log("In Test Shipment" + lotNumber );

                    shippedLot = (ShippedLot) m_shipDataMap.get(lotNumber + partNumber  );

                    if (shippedLot == null)
                    {
                        shippedLot = new ShippedLot( VENDOR_NAME_ISPL, WIP_TYPE_TEST );
                        m_shipDataMap.put(lotNumber + partNumber , shippedLot);
                    }

                    int quantity = 0;
                    if (shippedLot.getShipQty() != null)
                    {
                        try
                        {
                            quantity = Integer.parseInt( shippedLot.getShipQty() );
                        }
                        catch (NumberFormatException npe) {
                        }
                        try
                        {
                            quantity += Integer.parseInt( wipStatus.getTestOutQty() );
                        }
                        catch (NumberFormatException npe) {
                        }
                    }
                    else
                    {
                        try
                        {
                            quantity = Integer.parseInt(wipStatus.getTestOutQty() );
                        }
                        catch (NumberFormatException npe) {
                        }
                    }
                    if ( quantity > 0 )
                    {
                        shippedLot.setLotNumber(lotNumber);
                        shippedLot.setPartNumber(partNumber);
                        shippedLot.setShipDate(wipStatus.getActualShipDate() );
                        shippedLot.setShipQty( quantity+"");
                        shippedLot.setProcessType( LOCATION_TYPE_FT );
                        shippedLot.setFabLotNumber( lotNumber );
                        shippedLot.setNextLocation( VENDOR_NAME_OSEP );
                        shippedLot.setPackageType( wipStatus.getPkgCode() );
                    }
                }
                /*Considering when location as TNR and Remarks not equal to For Pack as TNR Lots*/
                if ( wipStatus.getLocation() != null && ( ( wipStatus.getLocation().equalsIgnoreCase("For TnR") || wipStatus.getLocation().equalsIgnoreCase("TnR") ) ||
							( wipStatus.getLocation().equalsIgnoreCase("For Test") && wipStatus.getForTnr() != null ) ) )
                {
                    log("In Tape and Reel" + lotNumber );

                    tnrLot = (InventoryLot) m_tnrDataMap.get(lotNumber + partNumber  );

                    if (tnrLot == null)
                    {
                        tnrLot = new InventoryLot(VENDOR_NAME_ISPL, LOCATION_TYPE_TAPE_REEL);
                        m_tnrDataMap.put(lotNumber + partNumber , tnrLot);
                    }

                    int quantity = 0;
                    if (tnrLot.getQtyIn() != null)
                    {
                        try
                        {
                            quantity = Integer.parseInt( tnrLot.getQtyIn() );
                        }
                        catch (NumberFormatException npe) {
                        }
                        try
                        {
                            quantity += Integer.parseInt( wipStatus.getForTnr() );
                        }
                        catch (NumberFormatException npe) {
                        }
                    }
                    else
                    {
                        try
                        {
                            quantity = Integer.parseInt(wipStatus.getForTnr() );
                        }
                        catch (NumberFormatException npe) {
                        }
                    }
                    
                    if ( quantity > 0 )
                    {
                        tnrLot.setLotNumber(lotNumber);
                        tnrLot.setPartNumber(partNumber);
                        tnrLot.setQtyIn(quantity+"");
                        if ( wipStatus.getCommitDate() != null )
                        {
                             tnrLot.setDateIn( wipStatus.getCommitDate() );
                        }
                        else if ( wipStatus.getExpectedDate() != null )
                        {
                            tnrLot.setDateIn( wipStatus.getExpectedDate() );
                        }
                        else
                        {
                            tnrLot.setDateIn( wipStatus.getReceiveDate() );
                        }

                        tnrLot.setStatus(LOT_STATUS_RUNNING);
                        tnrLot.setFabLotNumber(lotNumber);
                        tnrLot.setPkgType(wipStatus.getPkgCode());
                        tnrLot.setVendorLot( releaseNumber );
                    }
                }
                /*Considering when location as Packing and Remarks  equal to For Pack as FG Lots*/
                if ( wipStatus.getLocation() != null && wipStatus.getLocation().equalsIgnoreCase("PACKING")
                     || ( wipStatus.getLocation() != null && wipStatus.getLocation().equalsIgnoreCase("TNR") && wipStatus.getRemarks() != null
                        && wipStatus.getRemarks().equalsIgnoreCase("For Pack") ) )
                {
                    log("In FG" + lotNumber );

                    fgLot = (InventoryLot) m_fgDataMap.get(lotNumber + partNumber  );

                    if (fgLot == null)
                    {
                        fgLot = new InventoryLot(VENDOR_NAME_ISPL, INVENTORY_TYPE_FINISHED_GOODS);
                        m_fgDataMap.put(lotNumber + partNumber , fgLot);
                    }

                    int quantity = 0;
                    if (fgLot.getQtyIn() != null)
                    {
                        try
                        {
                            quantity = Integer.parseInt( fgLot.getQtyIn() );
                        }
                        catch (NumberFormatException npe) {
                        }
                        try
                        {
                            quantity += Integer.parseInt( wipStatus.getForPack() );
                        }
                        catch (NumberFormatException npe) {
                        }
                    }
                    else
                    {
                        try
                        {
                            quantity = Integer.parseInt(wipStatus.getForPack() );
                        }
                        catch (NumberFormatException npe) {
                        }
                    }

                    fgLot.setLotNumber(lotNumber);
                    fgLot.setPartNumber(partNumber);
                    fgLot.setQtyIn(quantity +"");
                    fgLot.setDateIn(wipStatus.getCommitDate());
                    fgLot.setStatus(LOT_STATUS_RUNNING);
                    fgLot.setFabLotNumber(lotNumber);
                    fgLot.setPkgType(wipStatus.getPkgCode());
                    fgLot.setLotCategory(LOT_CATEGORY_TYPE_PROD);
                    fgLot.setVendorLot( releaseNumber );

                }
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
            log("ERROR!ERROR! While parsing the ISPL TEST Details" + e);
            System.out.println("ERROR!ERROR! While parsing the ISPL TEST Details" + e);
            throw e;
        }
    }

    private void log(String message)
    {
        if (message == null) return;

        if (m_parserLogger != null)
        {
            synchronized (m_parserLogger)
            {
                m_parserLogger.logDebug(message + LINE_SEPARATOR);
            }
        }
        else
        {
            System.out.println(message);
        }
    }
}
